package cn.k.controller;

import cn.k.domain.Employee;
import cn.k.domain.Permission;
import cn.k.domain.Role;
import cn.k.qo.EmployeeQueryObject;
import cn.k.qo.JSONResult;
import cn.k.qo.PageResult;
import cn.k.qo.QueryObject;
import cn.k.service.IDepartmentService;
import cn.k.service.IEmployeeService;
import cn.k.service.IRoleService;
import cn.k.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    //处理分页查询页面请求
    @RequestMapping("/list")
    @RequirePermission(name = "员工查询", expression = "employee:list")
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //查询部门所有数据封装到Model中
        model.addAttribute("departments", departmentService.selectAll());

        return "employee/list";
    }


    //处理删除请求
    @RequestMapping("/delete")
    @RequirePermission(name = "员工删除", expression = "employee:delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }

    //处理添加或者更新的请求,跳转页面
    @RequestMapping("/input")
    @RequirePermission(name = "员工编辑", expression = "employee:saveOrUpdate")
    public String input(Long id, Model model) {
        //查询所有部门数据存到Model中
        model.addAttribute("departments", departmentService.selectAll());
        //查询所有角色存到model中
        model.addAttribute("roles", roleService.selectAll());

        //修改,带id值
        if (id != null) {
            model.addAttribute("employee", employeeService.get(id));
            //查询已经分配的角色
            List<Role> haveRoles = roleService.queryByEmployeeId(id);
            model.addAttribute("haveRoles", haveRoles);
        }
        return "employee/input";
    }

    //处理添加或者更新的请求
    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "员工编辑", expression = "employee:saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Employee employee, Long[] roleIds) {
        try {
            //判断有没有id
            if (employee.getId() == null) {
                employeeService.insert(employee, roleIds);
            } else {
                employeeService.update(employee, roleIds);
            }
            return new JSONResult(true, "编辑成功");
        } catch (Exception e) {
            return new JSONResult(false, "编辑失败");

        }
    }

    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String, Boolean> checkUsername(String username) {
        //若存在返回true ,若不存在返回false
        boolean exist = employeeService.checkUsername(username); //true
        Map<String, Boolean> data = new HashMap<>();
        data.put("valid", !exist);
        return data;
    }

    //导出excel.xls格式
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        //获取响应对象输出流,这样才可以把相应给用户下载文件写会浏览器
        workbook.write(response.getOutputStream());

    }

    //处理导入员工请求,本质就是上传
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        //根据用户上传的文件创建workbook对象
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        employeeService.importXls(wb);
        return "redirect:/employee/list";
    }


}
